package com.css.gdzj.gdzjbxgd.modules.reportinfo.controller;

import com.css.gdzj.gdzjbxgd.common.util.R;
import com.css.gdzj.gdzjbxgd.common.util.StringHelper;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.repository.BxgdReportContralRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.BxgdReportInfoRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.BxgdReportInfoEntityVo;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestGiftChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestPtjobChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.BxgdReportInfoService;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.HonestGiftChildService;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.service.HonestPtjobChildService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxx
 * @date 2020-12-11
 */
@Slf4j
@RestController
@RequestMapping("/bxgdReportInfos")
public class BxgdReportInfoController {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private BxgdReportInfoService bxgdReportInfoService;
    @Autowired
    private HonestGiftChildService honestGiftChildService;
    @Autowired
    private HonestPtjobChildService honestPtjobChildService;
    @Autowired
    private BxgdReportInfoRepository bxgdReportInfoRepository;
    @Autowired
    private BxgdReportContralRepository bxgdReportContralRepository;

    @GetMapping("/queryBxgdReportInfos")
    public R queryBxgdReportInfos(@RequestParam(value = "reportYear", required = false) String reportYear,
                                  @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        try {
            List<BxgdReportInfoEntity> bxgdInfoList = bxgdReportInfoService.queryBxgdReportInfos(reportYear, pageNo, pageSize);
            List<BxgdReportInfoEntity> bxgdInfoCount = bxgdReportInfoService.queryBxgdReportInfosCount(reportYear);
            List<BxgdReportInfoEntityVo> list = new ArrayList<>();
            String reportId = null;
            int total = 0;
            if (null != bxgdInfoList && bxgdInfoList.size() >0){
                for (BxgdReportInfoEntity bxgdReportInfoEntity : bxgdInfoList) {
                    BxgdReportInfoEntityVo bxgdReportInfoEntityVo = new BxgdReportInfoEntityVo();
                    bxgdReportInfoEntityVo.setBxgdReportInfoEntity(bxgdReportInfoEntity);
                    reportId = bxgdReportInfoEntity.getReportId();
                    if (bxgdReportInfoEntity.getHonestGift().equals("1")) {
                        List<HonestGiftChildEntity> giftChildEntities = honestGiftChildService.getList(reportId);
                        bxgdReportInfoEntityVo.setHonestGiftChildEntityList(giftChildEntities);
                    }
                    if (bxgdReportInfoEntity.getHonestPtjob().equals("1")) {
                        List<HonestPtjobChildEntity> ptjobChildEntities = honestPtjobChildService.getList(reportId);
                        bxgdReportInfoEntityVo.setHonestPtjobChildEntityList(ptjobChildEntities);
                    }
                    list.add(bxgdReportInfoEntityVo);
                }
            }
            if (null != bxgdInfoCount && bxgdInfoCount.size() >0){
                total = bxgdInfoCount.size();
            }
            return R.ok().put("data",list).put("total",total);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return R.error();
        }

    }

    @GetMapping("/getBxgdReportInfo")
    public R getBxgdReportInfo(@RequestParam(value = "id") String id) {
        try {
            BxgdReportInfoEntityVo bxgdReportInfoVo = bxgdReportInfoService.getBxgdReportInfo(id);
            return R.ok().put("data", bxgdReportInfoVo);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }

    @GetMapping("/getUserIsReport")
    public R getUserIsReport(@RequestParam(value = "userId") String userId) {
        boolean isReport = false;
        try {
            String reportYear = bxgdReportContralRepository.queryReportYear();
            int count = bxgdReportInfoRepository.getUserReportCount(userId, reportYear);
            if (count >0){
                isReport = true;
            }
            return R.ok().put("isReport", isReport);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }

    @PostMapping("/saveBxgdReportInfo")
    public R saveBxgdReportInfo(@RequestBody BxgdReportInfoEntityVo bxgdReportInfoVo) {
        try {
            bxgdReportInfoService.saveBxgdReportInfo(bxgdReportInfoVo);
            return R.ok();
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            return R.error();
        }

    }

    @PostMapping("/updateBxgdReportInfo")
    public R updateBxgdReportInfo(@RequestBody BxgdReportInfoEntityVo bxgdReportInfoVo) {
        try {
            bxgdReportInfoService.updateBxgdReportInfo(bxgdReportInfoVo);
            return R.ok();
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }

    @DeleteMapping("/deleteBxgdReportInfos")
    public R deleteBxgdReportInfos(@RequestBody List<String> ids) {
        try {
            bxgdReportInfoService.deleteBxgdReportInfos(ids);
            return R.ok();
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return R.error();
        }
    }



    /*@GetMapping("/exportExcel")
    public RestResponse exportFile(BxgdReportInfoQueryParams queryParams, HttpServletResponse response){
    RestResponse restResponse = null;
    JSONObject jsonObject = new JSONObject();
    try {
    bxgdReportInfoService.exportExcel(queryParams,response);
    jsonObject.put("message","导出成功");
    }catch (Exception e){
    String errorMsg = "导出失败";
    log.error("导出失败!",e);
    restResponse = RestResponse.fail(errorMsg);

    }
    return restResponse;
    }*/

   /* @GetMapping("/deptTemplate")
    public RestResponse downloadTemplate(){
    RestResponse restResponse = null;
    JSONObject jsonObject = new JSONObject();
    try{
    String fileName = "BxgdReportInfo.xlsx";
    platformDownloadUtils.download("filestemplate",fileName);
    jsonObject.put("message","模板下载成功");
    }catch (Exception e){
    log.error("下载失败");
    String message = "下载失败";
    restResponse = RestResponse.fail(message);

    }
    return restResponse;
    }
*/
    /*@PostMapping("/uploadFile")
    public RestResponse uploadFile(MultipartFile file, HttpServletResponse response){
    RestResponse restResponse = null;
    try{
    String returnMsg =  bxgdReportInfoService.uploadFile(file,response);
    restResponse = RestResponse.succeed(returnMsg);
    }catch (Exception e){
    String errorMessage = "导入失败!";
    log.error(errorMessage,e);
    restResponse = RestResponse.fail(errorMessage);

    }
    return  restResponse;
    }*/
}
